package com.mylove.floatinginputbox;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.benyanyi.floatinginputbox.FloatingInputBoxActivity;
import com.benyanyi.floatinginputbox.FloatingInputBoxDialog;
import com.benyanyi.floatinginputbox.callback.DefaultEditorCallback;
import com.benyanyi.floatinginputbox.callback.EditorCallback;

public class MainActivity extends AppCompatActivity {

    private Context context;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        tv = findViewById(R.id.msg);
        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                FloatingInputBoxActivity.openDefaultInputBox(context, new DefaultEditorCallback() {
//                    @Override
//                    public void onCancel() {
//                        toast("取消");
//                    }
//
//                    @Override
//                    public void onSubmit(String content) {
//                        tv.setText(content);
//                    }
//
//                });
                FloatingInputBoxDialog.openDefaultInputBox(context, new DefaultEditorCallback() {
                    @Override
                    public void onCancel() {
                        toast("取消");
                    }

                    @Override
                    public void onSubmit(String content) {
                        tv.setText(content);
                    }
                });
            }
        });
        findViewById(R.id.send2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FloatingInputBoxActivity.openInputBox(context, R.layout.edit, new EditorCallback() {
                    @Override
                    public EditText callback(final Activity activity) {
                        final EditText editText = activity.findViewById(R.id.edit);
                        activity.findViewById(R.id.but_send).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String s = editText.getText().toString();
                                tv.setText(s);
                                activity.finish();
                            }
                        });
                        return editText;
                    }
                });
            }
        });
    }

    private void toast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

}
